package com.ruoyi.site.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.site.domain.Tracks;
import com.ruoyi.site.service.ITracksService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.site.domain.Playlists;
import com.ruoyi.site.service.IPlaylistsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 播放列Controller
 * 
 * @author chuckeytang
 * @date 2025-08-17
 */
@RestController
@RequestMapping("/site/playlists")
public class PlaylistsController extends BaseController
{
    @Autowired
    private IPlaylistsService playlistsService;

    @Autowired
    private ITracksService tracksService;

    /**
     * 查询播放列列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Playlists playlists)
    {
        startPage();
        List<Playlists> list = playlistsService.selectPlaylistsList(playlists);
        return getDataTable(list);
    }

    /**
     * 导出播放列列表
     */
    @Log(title = "播放列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Playlists playlists)
    {
        List<Playlists> list = playlistsService.selectPlaylistsList(playlists);
        ExcelUtil<Playlists> util = new ExcelUtil<Playlists>(Playlists.class);
        util.exportExcel(response, list, "播放列数据");
    }

    /**
     * 获取播放列详细信息
     */
    @GetMapping(value = "/{playlistId}")
    public AjaxResult getInfo(@PathVariable("playlistId") Long playlistId)
    {
        return success(playlistsService.selectPlaylistsByPlaylistId(playlistId));
    }

    /**
     * 新增播放列
     */
    @PreAuthorize("@ss.hasPermi('site:playlists:add')")
    @Log(title = "播放列", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Playlists playlists)
    {
        return toAjax(playlistsService.insertPlaylists(playlists));
    }

    /**
     * 修改播放列
     */
    @PreAuthorize("@ss.hasPermi('site:playlists:edit')")
    @Log(title = "播放列", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Playlists playlists)
    {
        return toAjax(playlistsService.updatePlaylists(playlists));
    }

    /**
     * 删除播放列
     */
    @PreAuthorize("@ss.hasPermi('site:playlists:remove')")
    @Log(title = "播放列", businessType = BusinessType.DELETE)
	@DeleteMapping("/{playlistIds}")
    public AjaxResult remove(@PathVariable Long[] playlistIds)
    {
        return toAjax(playlistsService.deletePlaylistsByPlaylistIds(playlistIds));
    }

    /**
     * 根据播放列表ID查询其下的音乐曲目列表
     */
    @GetMapping("/{playlistId}/tracks")
    public TableDataInfo getPlaylistsMusic(@PathVariable("playlistId") Long playlistId) {
        startPage();
        // 调用 Service 层方法，传入播放列表ID进行查询
        List<Tracks> list = playlistsService.selectTracksByPlaylistId(playlistId);
        return getDataTable(list);
    }
}
